package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import com.util.PageBean;
import com.util.removeHTML;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/students")
public class StudentsController {

	@Resource
	private StudentsService studentsService;

	@Resource
	private RecordsService recordsService;

	@Resource
	private OrdersService ordersService;

	// 用户列表
	@RequestMapping(value = "/list")
	@CrossOrigin
	public Response<List<Students>> list(@RequestBody Students students, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = studentsService.getCount(students);
		// 获取当前页记录
		List<Students> studentsList = studentsService.queryStudentsList(students, page);

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(studentsList, counts, page_count);
	}

	// 用户列表
	@RequestMapping(value = "/list2")
	@CrossOrigin
	public Response<List<Students>> list2(Students students, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = studentsService.getCount(students);
		// 获取当前页记录
		List<Students> studentsList = studentsService.queryStudentsList(students, page);

		String f = req.getParameter("f");
		if (f != null && f.equals("3")) {
		
			//遍历
			for (Students student : studentsList) {
				//共完成多少单
				Orders orders = new Orders();
				orders.setSno(student.getSno());
				orders.setFlag("已完成");
				int count = ordersService.getCount(orders);
				student.setBy1(count + "");			
				
			}
		}

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(studentsList, counts, page_count);
	}

	// 添加用户
	@ResponseBody
	@PostMapping(value = "/add")
	@CrossOrigin
	public Response add(Students students, HttpServletRequest req) throws Exception {
		try {
			// 判断用户名是否存在
			Students students1 = new Students();
			students1.setSno(students.getSno());
			List<Students> studentsList = studentsService.queryStudentsList(students1, null);
			if (studentsList.size() > 0) {
				return Response.error(201, "用户名已存在，请重新输入");
			} else {
				studentsService.insertStudents(students); // 添加
			}

		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 删除用户
	@ResponseBody
	@PostMapping(value = "/del")
	@CrossOrigin
	public Response del(HttpServletRequest req) throws Exception {
		try {
			String id = req.getParameter("id");
			studentsService.deleteStudents(id); // 删除
		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 修改用户
	@ResponseBody
	@PostMapping(value = "/update")
	@CrossOrigin
	public Response update(@RequestBody Students students, HttpServletRequest req) throws Exception {
		try {
			studentsService.updateStudents(students); // 修改
		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 修改用户
	@ResponseBody
	@PostMapping(value = "/update2")
	@CrossOrigin
	public Response update2(Students students, HttpServletRequest req) throws Exception {
		try {

			String f = req.getParameter("f");
			if (f != null && f.equals("1")) {
				studentsService.updateStudents(students); // 修改
			} else if (f != null && f.equals("2")) {
				
				Double pmoney = Double.parseDouble(req.getParameter("pmoney"));
				// 充值
				Students students2 = studentsService.queryStudentsById(students.getSno());
				students2.setUval(Double.parseDouble(students2.getUval().toString()) + pmoney);

				studentsService.updateStudents(students2);

				Records records = new Records();
				records.setSno(students2.getSno());
				records.setRtype("收入");
				records.setRmemo("通过微信充值" + pmoney + "元");

				recordsService.insertRecords(records);

			} else if (f != null && f.equals("3")) {
				
				//提现
				Double pmoney = Double.parseDouble(req.getParameter("pmoney"));
				// 提现
				Students students2 = studentsService.queryStudentsById(students.getSno());
				students2.setUval(Double.parseDouble(students2.getUval().toString()) - pmoney);

				studentsService.updateStudents(students2);

				Records records = new Records();
				records.setSno(students2.getSno());
				records.setRtype("支出");
				records.setRmemo("通过微信提现" + pmoney + "元");

				recordsService.insertRecords(records);

			} else {
				studentsService.updateStudents(students); // 修改
			}

		} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}

	// 返回用户详情
	@ResponseBody
	@PostMapping(value = "/get")
	@CrossOrigin
	public Response get(HttpServletRequest req) throws Exception {
		try {
			String id = req.getParameter("id");
			Students students = studentsService.queryStudentsById(id); // 根据ID查询
			return Response.success(students);
		} catch (Exception e) {
			return Response.error();
		}

	}

	// 用户登录
	@RequestMapping(value = "/usersLogin")
	@ResponseBody
	public String usersLogin(HttpServletRequest req) throws Exception {

		String msg = "";
		Students students = new Students();
		students.setSno(req.getParameter("lname"));

		List<Students> studentsList = studentsService.queryStudentsList(students, null);
		if (studentsList != null && studentsList.size() > 0) {

			// 判断用户状态
			if (studentsList.get(0).getFlag().equals("禁用")) {
				{
					msg = "2";
				}
			}

		} else {
			msg = "1";
		}
		return msg;
	}

	// 用户注册
	@RequestMapping(value = "/users_Add")
	@ResponseBody
	public String users_Add(Students students, HttpServletRequest request) throws Exception {

		String msg = "";
		Students students2 = new Students();
		students2.setSno(students.getSno());

		List<Students> list = studentsService.queryStudentsList(students2, null);

		if (list != null && list.size() > 0) {
			msg = "1";
		} else {

			studentsService.insertStudents(students);

		}

		return msg;
	}

	// 修改密码
	@RequestMapping(value = "/users_Pass")
	@ResponseBody
	public String users_Pass(HttpServletRequest req) throws Exception {

		String msg = "";
		String pwd1 = req.getParameter("pass1");
		String pwd2 = req.getParameter("pass2");
		String pwd3 = req.getParameter("pass3");

		Students students = new Students();
		students.setSno(req.getParameter("lname"));

		List<Students> mList = studentsService.queryStudentsList(students, null);
		if (mList != null && mList.size() > 0) {
			students.setPass(pwd2);
			studentsService.updateStudents(students);

		} else {
			msg = "1";

		}
		return msg;
	}

}
